using System;

namespace LaplaceMeshDeformer
{
    /// <summary>
    /// 应用程序配置类
    /// 包含拉普拉斯变形相关的配置参数
    /// </summary>
    public static class Config
    {
        /// <summary>
        /// 线性系统求解相关配置
        /// </summary>
        public static class LinearSolver
        {
            /// <summary>
            /// 共轭梯度法最大迭代次数
            /// </summary>
            public static int MaxIterations = 1000;

            /// <summary>
            /// 共轭梯度法收敛阈值
            /// </summary>
            public static double Tolerance = 1e-6;
        }

        /// <summary>
        /// 变形相关配置
        /// </summary>
        public static class Deformation
        {
            /// <summary>
            /// 默认使用余切权重
            /// </summary>
            public static bool UseCotangentWeights = false;

            /// <summary>
            /// 平滑操作默认迭代次数
            /// </summary>
            public static int DefaultSmoothIterations = 5;

            /// <summary>
            /// 平滑操作默认强度
            /// </summary>
            public static double DefaultSmoothLambda = 0.5;

            /// <summary>
            /// 区域变形默认影响半径
            /// </summary>
            public static double DefaultRegionRadius = 1.0;

            /// <summary>
            /// 区域变形默认缩放因子
            /// </summary>
            public static double DefaultScaleFactor = 1.5;
        }

        /// <summary>
        /// 网格处理相关配置
        /// </summary>
        public static class MeshProcessing
        {
            /// <summary>
            /// 边缘提取阈值角度（度）
            /// 当两个面的法线夹角大于此值时，认为其共享边为特征边
            /// </summary>
            public static double FeatureEdgeAngleThreshold = 30.0;

            /// <summary>
            /// 网格简化后的目标面数百分比
            /// </summary>
            public static double SimplificationTargetRatio = 0.5;

            /// <summary>
            /// 拓扑错误修复迭代次数上限
            /// </summary>
            public static int TopologyFixMaxIterations = 10;
        }

        /// <summary>
        /// UI相关配置
        /// </summary>
        public static class UI
        {
            /// <summary>
            /// 选中点的渲染大小
            /// </summary>
            public static double SelectedPointSize = 5.0;

            /// <summary>
            /// 特征边的渲染宽度
            /// </summary>
            public static double FeatureEdgeWidth = 2.0;

            /// <summary>
            /// 锚点颜色
            /// </summary>
            public static readonly uint AnchorPointColor = 0xFF0000FF; // 红色

            /// <summary>
            /// 控制点颜色
            /// </summary>
            public static readonly uint ControlPointColor = 0xFFFF00FF; // 洋红色

            /// <summary>
            /// 默认网格颜色
            /// </summary>
            public static readonly uint DefaultMeshColor = 0xCCCCCCFF; // 浅灰色
        }

        /// <summary>
        /// 文件相关配置
        /// </summary>
        public static class FileIO
        {
            /// <summary>
            /// 支持的网格文件扩展名
            /// </summary>
            public static readonly string[] SupportedMeshExtensions = { ".obj" };

            /// <summary>
            /// 默认网格文件扩展名
            /// </summary>
            public static readonly string DefaultMeshExtension = ".obj";

            /// <summary>
            /// 保存变形后网格的默认文件名模板
            /// </summary>
            public static readonly string DeformedMeshFilenameTemplate = "{0}_deformed{1}";
        }

        /// <summary>
        /// 初始化配置
        /// </summary>
        public static void Initialize()
        {
            // 读取环境变量或配置文件中的设置（如果需要）
            string maxIterStr = Environment.GetEnvironmentVariable("LAPLACE_MAX_ITERATIONS");
            if (!string.IsNullOrEmpty(maxIterStr) && int.TryParse(maxIterStr, out int maxIter))
            {
                LinearSolver.MaxIterations = maxIter;
            }

            string tolStr = Environment.GetEnvironmentVariable("LAPLACE_TOLERANCE");
            if (!string.IsNullOrEmpty(tolStr) && double.TryParse(tolStr, out double tol))
            {
                LinearSolver.Tolerance = tol;
            }

            string useCotangentStr = Environment.GetEnvironmentVariable("LAPLACE_USE_COTANGENT");
            if (!string.IsNullOrEmpty(useCotangentStr) && bool.TryParse(useCotangentStr, out bool useCotangent))
            {
                Deformation.UseCotangentWeights = useCotangent;
            }
        }

        /// <summary>
        /// 根据输入文件名生成输出文件名
        /// </summary>
        /// <param name="inputFilename">输入文件名</param>
        /// <returns>输出文件名</returns>
        public static string GenerateOutputFilename(string inputFilename)
        {
            if (string.IsNullOrEmpty(inputFilename))
            {
                return "deformed.obj";
            }

            string extension = System.IO.Path.GetExtension(inputFilename);
            string nameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(inputFilename);
            
            if (string.IsNullOrEmpty(extension))
            {
                extension = Config.FileIO.DefaultMeshExtension;
            }

            return string.Format(FileIO.DeformedMeshFilenameTemplate, nameWithoutExtension, extension);
        }
    }
} 